package com.cheyun.netsalev3.repository

import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.cheyun.netsalev3.MyApplication
import com.cheyun.netsalev3.bean.dm.*
import com.cheyun.netsalev3.utils.api.*
import com.cheyun.netsalev3.utils.api.dm.DmApiCallback
import com.cheyun.netsalev3.utils.api.dm.DmApiException
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

class DmClueDetailsRepository(var id:String) {

    fun getDetail(callback: (data: DmClueList,history:DmHistory?) -> Unit) {
        var json = JSONObject()
        json.put("detail",1)
        json.put("clueId",id)
        DmApiClient.getInstance().getClueDetail(json.toString(),1, 1)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<DmClue,DmLastHistory>>() {
                    override fun onSuccess(t: DmBaseResp<DmClue, DmLastHistory>) {
                        t.list?.rows?.apply {
                            callback(t.list!!.rows[0],t.data!!.last_history)
                        }

                        Log.e("onSuccess", "onSuccess.....")
                    }

                    override fun onError(ex: DmApiException) {

                    }

                })
        Log.e("getInstance", "retun.....")
    }
    /**
     * 获取记录
     */
    fun getRecords(callback:(List<DmHistory>)-> Unit) {
        DmApiClient.getInstance().getRecords(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<List<DmHistory>,Any>>() {
                    override fun onSuccess(t: DmBaseResp<List<DmHistory>,Any>) {
                        t.list?.apply {
                            callback(t.list!!)
                        }
                    }
                    override fun onError(ex: DmApiException) {

                    }
                })
    }

    fun callPhone(clueId: Int, moth: (tel:String) -> Unit) {
        DmApiClient.getInstance().callPhone(clueId)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<String,Any>>() {
                    override fun onSuccess(t: DmBaseResp<String,Any>) {
                        moth(t.list!!)
                    }

                    override fun onError(ex: DmApiException) {
                        Looper.prepare()
                        Toast.makeText(MyApplication.getInstance(), ex.getMessage(), Toast.LENGTH_LONG).show()
                        Looper.loop()
                    }


                })
    }

    /**
     * 线索移交
     */
    fun clueTransfer(toUserId:String,toUser:String,remark:String, method: () -> Unit) {
        DmApiClient.getInstance().clueTransfer(id,toUserId,toUser,remark)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<Int,Any>>() {
                    override fun onSuccess(t: DmBaseResp<Int,Any>) {
                        method()
                    }
                    override fun onError(ex: DmApiException) {

                    }
                })
    }

    fun getDealusers(method: (List<DmDealUser>) -> Unit){
        DmApiClient.getInstance().getDealUsers()
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<List<DmDealUser>,Any>>() {
                    override fun onSuccess(t: DmBaseResp<List<DmDealUser>,Any>) {
                        t.list?.apply {
                            method(t.list!!)
                        }
                    }
                    override fun onError(ex: DmApiException) {

                    }
                })
    }
}